/*
 *  Copyright 2017 Google Inc. All Rights Reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.google.blockly.ohos;

import com.google.blockly.ohos.control.BlocklyController;
import com.google.blockly.ohos.ui.*;
import com.google.blockly.model.BlocklyCategory;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;

/**
 * Creates a set of tabs of Blockly categories. The set of categories should be provided by
 * {@link #setContents(BlocklyCategory)}. Interaction with other fractions is not handled by this
 * class and should be done by registering a callback with
 * {@link #setCategoryCallback(CategorySelectorUI.Callback)}.
 */
public class CategorySelectorFraction extends Fraction implements CategorySelectorUI {

    private static final String TAG = "CategorySelectorFraction";

    protected CategoryView mCategoryView;
    protected WorkspaceHelper mHelper;
    protected BlocklyController mController;

    protected int mScrollOrientation;

    public CategorySelectorFraction(int scrollOrientation) {
        mScrollOrientation = scrollOrientation;
    }

    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        int layout = mScrollOrientation == Component.VERTICAL
                ? ResourceTable.Layout_default_category_start : ResourceTable.Layout_default_category_horizontal;
        Component rootView = scatter.parse(layout, container, false);
        mCategoryView = (CategoryView) rootView.findComponentById(ResourceTable.Id_category_view);
        mCategoryView.setCategoryTabs((CategoryTabs) rootView.findComponentById(ResourceTable.Id_category_tabs));
        mCategoryView.setScrollOrientation(mScrollOrientation);
        return mCategoryView;
    }

    public void setContents(BlocklyCategory rootCategory) {
        mCategoryView.setContents(rootCategory);
    }

    public void setCurrentCategory(BlocklyCategory category) {
        mCategoryView.setCurrentCategory(category);
    }

    public BlocklyCategory getCurrentCategory() {
        return mCategoryView.getCurrentCategory();
    }

    public void setCategoryCallback(CategorySelectorUI.Callback categoryCallback) {
        mCategoryView.setCallback(categoryCallback);
    }
}
